package com.ruoyi.zsix.service.impl;

import com.ruoyi.common.core.domain.zsixOrg.GroupEntity;
import com.ruoyi.common.core.domain.zsixOrg.PositionEntity;
import com.ruoyi.common.core.domain.zsixOrg.RolesEntity;
import com.ruoyi.common.core.domain.zsixOrg.UserEntity;
import com.ruoyi.zsix.domain.model.GroupUsersParams;
import com.ruoyi.zsix.domain.orgmanagement.DepartmentEntity;
import com.ruoyi.zsix.mapper.orgmanagementMapper.*;
import com.ruoyi.zsix.service.IOrgManagementService;
import com.ruoyi.zsix.service.manager.CommManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class IOrgManagementServiceImpl implements IOrgManagementService {
    @Autowired
    private CommManager commManager;
    @Autowired
    private UserEntityMapper userEntityMapper;
    @Autowired
    private DepartmentEntityMapper departmentEntityMapper;
    @Autowired
    private RolesEntityMapper rolesEntityMapper;
    @Autowired
    private PositionEntityMapper positionEntityMapper;
    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private UserGroupMapper userGroupMapper;
    /**
     * 获取组织结构数据
     * @return
     */
    @Override
    public HashMap<String,Object> getOrgData(){
        List<DepartmentEntity> departmentList=getDepartmentEntityList();
        //List<UserEntity> userEntityList=userEntityMapper.getUserEntityList("","","all");
        List<RolesEntity> rolesEntities=rolesEntityMapper.rolesList("");
        List<PositionEntity> positionEntities=positionEntityMapper.positionList("");
//        List<CompanyEntity> companyEntities=getCompanyEntityList();
        List<GroupEntity> groupEntityList=groupMapper.getGroupList("");
        HashMap<String,Object> hashMap=new HashMap<>();

        hashMap.put("departList",departmentList);
        //hashMap.put("userList",userEntityList);
        hashMap.put("rolesList",rolesEntities);
        hashMap.put("positionList",positionEntities);
//        hashMap.put("companyList",companyEntities);
        hashMap.put("groupEntityList",groupEntityList);

        return hashMap;
    }
    /**
     * 根据部门编号查找人员
     * @param departid
     * @return
     */
    @Override
    public List<UserEntity> getUserByDepartId(Long departid){
        return userEntityMapper.getUserByDepartId(departid);
    }
    /**
     * 获取部门树
     * @return
     */
    @Override
    public List<DepartmentEntity> getDepartmentEntityList(){

        if(!commManager.getUserNo().equals("admin")){
            UserEntity userEntity=userEntityMapper.getUserEntityInfo(commManager.getUserNo());
            List<DepartmentEntity> departmentList= departmentEntityMapper.getDepartmentList(userEntity.getDepartid().toString(),"");
            //2、组装树形结构
            List<DepartmentEntity> newList = departmentList.stream()
                    .filter(t -> t.getParentId() == userEntity.getDepartid()||t.getId() == userEntity.getDepartid())
                    .map((menu) -> {
                        menu.setChildren(commManager.getDeptChildren(menu,departmentList));
                        return menu;
                    })
                    .collect(Collectors.toList());
            return newList;
        }
        else{
            List<DepartmentEntity> departmentList= departmentEntityMapper.getDepartmentList("","");
            //2、组装树形结构
            List<DepartmentEntity> newList = departmentList.stream()
                    .filter(t -> t.getParentId() == 0)
                    .map((menu) -> {
                        menu.setChildren(commManager.getDeptChildren(menu,departmentList));
                        return menu;
                    })
                    .collect(Collectors.toList());
            return newList;
        }

    }

    /**
     * 获取用户分组列表
     * @param groupEntity
     * @return
     */
    @Override
    public List<GroupEntity> getGroupList(GroupEntity groupEntity){
        return groupMapper.getGroupList(groupEntity.getName());
    }
    /**
     * 获取分组内成员
     * @param
     * @return
     */
    @Override
    public List<UserEntity> getUserByGorupId(GroupEntity groupEntity){
        List<UserEntity> userEntities = groupMapper.getUserByGorupId(groupEntity.getId());
        return userEntities;
    }
    /**
     * 增加分组
     * @param groupEntity
     * @return
     */
    @Override
    public GroupEntity addGroup(GroupEntity groupEntity){
        groupMapper.addGroup(groupEntity);
        return groupEntity;
    }
    /**
     * 更新分组
     * @param groupEntity
     * @return
     */
    @Override
    public GroupEntity updataGroup(GroupEntity groupEntity){
        groupMapper.updataGroup(groupEntity);
        return groupEntity;
    }
    /**
     * 删除分组
     * @param groupEntity
     * @return
     */
    @Override
    public GroupEntity deleteGroup(GroupEntity groupEntity){
        groupMapper.deleteGroup(groupEntity.getId());
        return groupEntity;
    }
    /**
     * 增加人员分组
     * @param param
     * @return
     */
    @Override
    public GroupUsersParams addUserGroup(GroupUsersParams param){
        List<HashMap<String,Object>> hashMapList=new ArrayList<>();
        if(param.getUserDatalist().size()==0)
        {
            for (UserEntity user : param.getUserlist()) {
                HashMap<String,Object> hashMap = new HashMap<>();
                hashMap.put("id", UUID.randomUUID().toString());
                hashMap.put("username", user.getUsername());
                hashMap.put("groupid", param.getGroupid());
                hashMapList.add(hashMap);
            }
        }
        else {
            for (UserEntity userEntity : param.getUserDatalist()) {
                for (UserEntity user : param.getUserlist()) {
                    if (userEntity.getUsername().equals(user.getUsername()))
                        continue;
                    else {
                        HashMap<String,Object> hashMap = new HashMap<>();
                        hashMap.put("id", UUID.randomUUID().toString());
                        hashMap.put("username", user.getUsername());
                        hashMap.put("groupid", param.getGroupid());
                        hashMapList.add(hashMap);
                    }
                }
            }
        }
        userGroupMapper.addUserGroup(hashMapList);
        return param;
    }
    /**
     * 删除分组人员
     * @param username
     * @param groupid
     * @return
     */
    @Override
    public String deleteUserGroup(String username, String groupid){
        userGroupMapper.deleteUserGroup(username,groupid);
        return "删除成功";
    }
}
